package highTech.proxy.proxyClass;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyProxy implements InvocationHandler{

	private Object target;
	private Advice advice;
	
	public MyProxy(Object target,Advice advice){
		this.target=target;
		this.advice=advice;
	}
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		if(advice!=null)
			advice.beforeMethod(method);
		Object retVal=method.invoke(target, args);
		if(advice!=null)
			advice.afterMethod(method);
		return retVal;
	}
	
	public Object getProxy(){
		return Proxy.newProxyInstance(
				this.target.getClass().getClassLoader(),
				this.target.getClass().getInterfaces(),
				this);
	}
	
	public Object getTarget() {
		return target;
	}

	public void setTarget(Object target) {
		this.target = target;
	}

	public Advice getAdvice() {
		return advice;
	}

	public void setAdvice(Advice advice) {
		this.advice = advice;
	}

}
